package com.fenghongzhang.lib_jetpack.recyclerview

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghongzhang.lib_jetpack.R

@Route(path = "/jetpack/recyclerview")
class RecyclerViewMainActivity : AppCompatActivity() {

    private lateinit var adapter: MultiTypeAdapter
    private lateinit var itemList: MutableList<Item>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.jetpack_activity_recycler_view_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        val recyclerView: RecyclerView = findViewById(R.id.rv)
        recyclerView.addItemDecoration(DividerItemDecoration(this))
        recyclerView.layoutManager = LinearLayoutManager(this)

         itemList = ArrayList<Item>()
           itemList.add(Item.Type1("Item 1"))
           itemList.add(Item.Type2("Item 2"))
           itemList.add(Item.Type1("Item 3"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))
           itemList.add(Item.Type2("Item 4"))

         adapter = MultiTypeAdapter(
            itemList,
            { item ->
                // 处理Item点击事件
                Toast.makeText(this, "Clicked:", Toast.LENGTH_SHORT).show()
            },{ item ->
                // 处理Item点击事件
                Toast.makeText(this, "长安的事件:", Toast.LENGTH_SHORT).show()
            })
        recyclerView.adapter = adapter
        recyclerView.setHasFixedSize(true)

        // 使用 PagerSnapHelper 实现整页翻页
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(recyclerView)


        // 创建 ItemTouchHelper 并绑定
        val callback = ItemTouchHelperCallback(adapter)
        val itemTouchHelper = ItemTouchHelper(callback)
        itemTouchHelper.attachToRecyclerView(recyclerView)


        // 监听滚动状态，控制视频播放
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

                // 当滑动停止时，执行播放逻辑
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    // 获取当前可见的项
                    val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                    val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

                    // 播放当前可见的第一个视频
                   // adapter.setCurrentPlayingPosition(firstVisibleItemPosition)
                    println("当前可见的第一个视频位置：$firstVisibleItemPosition")
                }
            }


            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

//                // 获取当前可见的项
//                val layoutManager = recyclerView.layoutManager as LinearLayoutManager
//                val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
//                val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
//
//                // 播放当前可见的第一个视频
//                val targetPosition = firstVisibleItemPosition
//                println("当前可见的第一个视频位置：$targetPosition")

//                if (targetPosition != adapter.currentPlayingPosition) {
//                    // 停止前一个视频
//                    adapter.setCurrentPlayingPosition(targetPosition)
//                }
            }
        })


    }

    fun refresh(view: View) {
        itemList.set(1, Item.Type1("Item 11"))
        adapter.notifyItemChanged(0, "123")
    }


}